<template>
  <div>
    <div class="button-box">
      <el-button type="primary" @click="dialogVisible = true">新增视频</el-button>
    </div>
    <div style="margin-left: 20px;">
      <el-row>
        <el-col :span="5" v-for="(video, index) in videoList" :key="video.id" :offset="index % 4 == 0 ? 0 : 1"
          style="margin-bottom: 15px;">
          <el-card :body-style="{ padding: '0px' }">
            <img src="@/assets/video.jpg" class="image" @click="$router.push('/train/video-play')">
            <div style="padding: 14px;display: flex;justify-content: space-between;">
              <div>{{ video.name }}</div>
              <el-popconfirm title="确定删除吗？" @onConfirm="deleteVideo(video.id)">
                <a class="delete-button" slot="reference">删除视频</a>
              </el-popconfirm>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </div>
    <el-dialog :visible.sync="dialogVisible" width="30%">
      <el-form :model="addForm" label-position="top">
        <el-form-item label="视频名称" label-width="120px">
          <el-input v-model="addForm.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="上传视频" label-width="120px">
          <el-upload ref="upload" drag action="https://www.demo.com" :auto-upload="false" :file-list="fileList">
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
          </el-upload>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancelAddVideo">取 消</el-button>
        <el-button type="primary" @click="addVideo">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      dialogVisible: false,
      videoList: [],
      addForm: {
        name: ''
      },
      fileList: []
    }
  },
  mounted() {
    this.getVideoList()
  },
  methods: {
    getVideoList() {
      this.axios.get('http://139.9.77.245:8081/train-video').then(res => {
        if (res.data.code === 0) {
          this.videoList = res.data.data
        } else {
          this.$message.error('获取视频列表失败')
        }
      })
    },
    addVideo() {
      this.$refs.upload.submit();
      this.axios.post('http://139.9.77.245:8081/train-video', this.addForm).then(res => {
        if (res.data.code === 0) {
          this.$message.success('添加成功')
          this.dialogVisible = false
          this.addForm = {
            name: ''
          }
          this.getVideoList()
        } else {
          this.$message.error(res.data.message)
        }
      })
    },
    cancelAddVideo() {
      this.dialogVisible = false
      this.addForm = {
        name: ''
      }
    },
    deleteVideo(id) {
      this.axios.delete('http://139.9.77.245:8081/train-video/' + id).then(res => {
        if (res.data.code === 0) {
          this.$message.success('删除成功')
          this.getVideoList()
        } else {
          this.$message.error(res.data.message)
        }
      })
    }
  }
}
</script>

<style scoped>
.button-box {
  padding: 15px 0 15px 40px;
}

.delete-button {
  font-size: 13px;
  color: #999;
}

.bottom {
  margin-top: 13px;
  line-height: 12px;
}

.button {
  padding: 0;
  float: right;
}

.image {
  width: 100%;
  display: block;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}

.clearfix:after {
  clear: both
}
</style>
